<?php

namespace App\Repository\Repositories;

use App\Repository\Models\Shop;
use App\Repository\Models\ShopRightAuth;
use App\Repository\Models\ShopRightMenu;
use App\Repository\Models\ShopRightOperation;
use App\Repository\Repositories\Interfaces\ShopEmployeeRepository;
use App\Repository\Models\ShopEmployee;

/**
 * Class ShopEmployeeRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ShopEmployeeRepositoryEloquent extends BaseRepository implements ShopEmployeeRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return ShopEmployee::class;
    }

    public function getSellerNav($user_id){
        $is_shoper=Shop::query()->where('sp_uid',$user_id)->get()->toArray();
        $menu_ahth=[];
        $operation=[];
        if(!empty($is_shoper)){
            $menu=ShopRightMenu::query()->where('status',1)->orderBy('id','asc')->get();
            foreach($menu as $k=>$v){
                if($v['pid']==0){
                    $menu_ahth[$v['id']]['p_menu']=$v;
                    $menu_ahth[$v['id']]['menu']=[];
                }else{
                    $menu_ahth[$v['pid']]['menu'][$v['id']]=$v;

                }
            }
            $operation=ShopRightOperation::query()->get();
        }else{
            $emp=ShopEmployee::query()->where('user_id',$user_id)->get()->toArray();

            if(empty($emp)){
                $menu=ShopRightMenu::query()->where('status',1)->orderBy('id','asc')->get();
                foreach($menu as $k=>$v){
                    if($v['pid']==0){
                        $menu_ahth[$v['id']]['p_menu']=$v;
                        $menu_ahth[$v['id']]['menu']=[];
                    }else{
                        $menu_ahth[$v['pid']]['menu'][$v['id']]=$v;

                    }
                }
                $operation=ShopRightOperation::query()->get();
            }else{
                $user_auth=ShopRightAuth::query()->where('emp_id',$emp[0]['id'])->get()->toArray();
                $menu_id=[];
                foreach($user_auth as $key=>$val){
                    if($val['resource_type']==1){
                        $menu_per=ShopRightMenu::query()->where('id',$val['resource_id'])->orderBy('id','asc')->get();
                        $menu_id[]=$menu_per[0];

                    }else{
                        $menu_oper=ShopRightOperation::query()->where('id',$val['resource_id'])->get();
                        $operation[]=$menu_oper[0];
                    }
                }
                array_multisort(array_column($menu_id,'pid'),SORT_ASC,$menu_id);
                foreach($menu_id as $k=>$v){
                    if($v['pid']==0){
                        $menu_ahth[$v['id']]['p_menu']=$v;
                        $menu_ahth[$v['id']]['menu']=[];
                    }else{
                        $menu_ahth[$v['pid']]['menu'][$v['id']]=$v;
                    }
                }
            }

        }

        $auth=[
            'menu'=>$menu_ahth,
            'operation'=>$operation
        ];

        return $auth;
//        ShopEmployee::query()->where('user_id',$user_id)->get();
    }
    
}
